package com.ja.openchat.ui.chat.adapter.viewholder;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.util.Pair;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.RecyclerView;

import com.ja.openchat.R;
import com.zy.openchat.core.model.message.MessageInfo;
import com.zy.openchat.core.model.message.UiMessage;
import com.ja.openchat.ui.chat.ConversationFragment;
import com.ja.openchat.ui.chat.MessageViewModel;
import com.ja.openchat.ui.chat.adapter.ConversationMessageAdapter;
import com.zy.openchat.util.TimeConvertUtils;
import com.zy.openchat.util.TimeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public abstract class MessageContentViewHolder extends RecyclerView.ViewHolder {
    @NonNull
    protected ConversationFragment fragment;
    public View itemView;
    protected UiMessage message;
    protected int position;
    protected RecyclerView.Adapter adapter;
    protected MessageViewModel messageViewModel;

    @BindView(R.id.timeTextView)
    TextView timeTextView;
    public int direction;

    public MessageInfo getMessageInfo(){
        return message!=null?message.message:null;
    }


    public MessageContentViewHolder(@NonNull ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
        super(itemView);
        this.fragment = fragment;
        this.itemView = itemView;
        this.adapter = adapter;
        messageViewModel = ViewModelProviders.of(fragment).get(MessageViewModel.class);
        ButterKnife.bind(this, itemView);
        itemView.setTag(this);
    }

    public void onBind(UiMessage message, int position,int payloadType) {
        onBind(message,position);
    }

    public void onBind(UiMessage message, int position) {
        setMessageTime(message.message, position);
        setReciveRead(message.message);
    }

    private void setReciveRead(MessageInfo messageInfo){
        if(messageInfo!=null&&!messageInfo.isOutgoing()&&fragment!=null){
            fragment.setReciveRead(messageInfo);
        }
    }

    /**
     * @param uiMessage
     * @param tag
     * @return 返回true，将从context menu中排除
     */

    public abstract boolean contextMenuItemFilter(UiMessage uiMessage, String tag);

    public abstract Pair<String,Integer> contextMenuTitle(Context context, String tag);

    public abstract String contextConfirmPrompt(Context context, String tag);

    public void onViewRecycled() {
        // you can do some clean up here
    }

    protected void setMessageTime(MessageInfo item, int position) {
        long msgTime = item.date*1000;
        if (position > 0) {
            MessageInfo preMsg = ((ConversationMessageAdapter) adapter).getItem(position - 1).message;
            long preMsgTime = preMsg.date*1000;
            if (!TimeConvertUtils.formatDate10(msgTime).equals(TimeConvertUtils.formatDate10(preMsgTime))) {
                timeTextView.setVisibility(View.VISIBLE);
                timeTextView.setText(TimeUtils.getMsgFormatTimeForYYMMDD(msgTime));
            } else {
                timeTextView.setVisibility(View.GONE);
            }
        } else {
            timeTextView.setVisibility(View.VISIBLE);
            timeTextView.setText(TimeUtils.getMsgFormatTimeForYYMMDD(msgTime));
        }
    }


    public void setDirection(int direction){
        this.direction = direction;
    }


    public void onMessageReactionClick(int reactionId){
        if(message!=null&&message.message!=null&&fragment!=null){
            fragment.sendReaction(message,reactionId);
        }
    }
}
